<?php
namespace Admin\Controller;
use Admin\Model\UserLogModel;

class UserController extends MainController{

    /**
     * 用户登录
     * @queyuan
     * @param $name/$phone,$password,$type,$ip
     * 2016/05/31
     */
    public function userLogin(){
        $name = I("param.name");
        $phone = I("param.phone");
        $password = I("param.password");
        $type = I("param.type");
        $ip = I("param.ip");
        $password = trim($password);
        $result = array();
        if((!$name && !$phone) || !$password || !$type || !$ip){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $password = md5($password);
        }
        if($name){
            $where = "name = '".$name."'";
        }else{
            $where = "phone = '".$phone."'";
        }
        $where .= " and password = '".$password."'";
        $user = M("user")->where($where)->field("id,name,phone,avatar,money,integral,avatar_type,birthday")->find();
        if(!$user){
            $result['status'] = 1;
            $result['msg'] = "用户名/密码不正确";
        }else{
            $data = array("logintime"=>time(),"loginip"=>$ip,"logintype"=>$type);
            $res = M("user")->where("id = ".$user['id'])->save($data);
            if($res){
                $log = new UserLogModel();
                $log->addLog($user['id'],"用户：".$user['name']."登录",0,$ip,"user");
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['user'] = $user;
            }else{
                $result['status'] = 1;
                $result['msg'] = "登录失败";
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 用户注册
     * @queyuan
     * 2016/05/31
     * @param $name,$phone,$password,$code,$ip,$type
     */
    public function userRegiest(){
        $name = I("param.name");
        $phone = I("param.phone");
        $password = I("param.password");
        $code = I("param.code");
        $ip = I("param.ip");
        $type = I("param.type");
        $password = trim($password);
        $result = array();
        if(!$name || !$phone || !$password || !$code || !$ip || !$type){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("phone = '".$phone."' and status = 1")->find();
            if($user){
                $result['status'] = 1;
                $result['msg'] = "手机号已注册";
            }else{
                $user = M("user")->where("name = '".$name."' and status = 1")->find();
                if($user){
                    $result['status'] = 1;
                    $result['msg'] = "昵称已注册";
                }else{
                    $m_password = md5($password);
                    $c = M("user_code")->where("phone = '".$phone."' and flag = 0")->order("add_time desc")->getField("code");
                    if($code != $c){
                        $result['status'] = 1;
                        $result['msg'] = "验证码有误";
                    }else{
                        $data = array(
                            'name'=>$name,
                            'phone'=>$phone,
                            'password'=>$m_password,
                            'addtime'=>time(),
                            'status'=>1,
                        );
                        $res = M("user")->add($data);
                        if($res){
                            $log = new UserLogModel();
                            $log->addLog($res,"用户：".$data['name']."注册",4,$ip,"user");
                            $result['status'] = 0;
                            $result['msg'] = "注册成功";
                            $result['user_id'] = $res;
                            $result['name'] = $data['name'];
                            $result['password'] = $password;
                        }else{
                            $result['status'] = 1;
                            $result['msg'] = "注册失败";
                        }
                    }
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 找回密码
     * @queyuan
     * 2016/05/31
     * @param $phone,$password,$ip,$code,$flag
     */
    public function findPassword(){
        $phone = I("param.phone");
        $password = I("param.password");
        $ip = I("param.ip");
        $code = I("param.code");
        $result = array();
        $password = trim($password);
        $log = new UserLogModel();
        if(!$phone || !$password || !$ip || !$code){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $c = M("user_code")->where("phone = '".$phone."' and flag = 2")->order("add_time desc")->getField("code");
            if($c != $code){
                $result['status'] = 1;
                $result['msg'] = "验证码错误";
            }else{
                $user = M("user")->where("phone = '".$phone."' and status = 1")->find();
                if(!$user){
                    $result['status'] = 1;
                    $result['msg'] = "手机号未注册";
                }else{
                    $password = md5($password);
                    if($password == $user['password']){
                        $result['status'] = 0;
                        $result['msg'] = "修改成功";
                        $log->addLog($user['id'],"用户：".$user['name']."找回密码",5,$ip,"user");
                    }else{
                        $data = array("password",$password);
                        $res = M("user")->where("id = ".$user['id'])->save($data);
                        if($res){
                            $log->addLog($user['id'],"用户：".$user['name']."找回密码",5,$ip,"user");
                            $result['status'] = 0;
                            $result['msg'] = "修改成功";
                        }else{
                            $result['status'] = 1;
                            $result['msg'] = "修改失败";
                        }
                    }
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 重置密码
     * @queyuan
     * 2016/05/31
     * @param $user_id,$password,$ip,$code
     */
    public function resetPassword(){
        $user_id = I("param.user_id");
        $password = I("param.password");
        $ip = I("param.ip");
        $code = I("param.code");
        $password = trim($password);
        $result = array();
        $log = new UserLogModel();
        if(!$user_id || !$password || !$ip || !$code){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("id = ".$user_id." and status = 1")->find();
            if(!$user){
                $result['status'] = 1;
                $result['msg'] = "无此用户";
            }else{
                $c = M("user_code")->where("phone = '".$user['phone']."' and flag = 3")->order("add_time desc")->getField("code");
                if($c != $code){
                    $result['status'] = 1;
                    $result['msg'] = "验证码错误";
                }else{
                    $password = md5($password);
                    if($password == $user['password']){
                        $result['status'] = 0;
                        $result['msg'] = "重置成功";
                        $log->addLog($user['id'],"用户：".$user['name']."重置密码",5,$ip,"user");
                    }else{
                        $data = array('password'=>$password);
                        $res = M("user")->where("id = ".$user['id'])->save($data);
                        if($res){
                            $log->addLog($user['id'],"用户：".$user['name']."重置密码",5,$ip,"user");
                            $result['status'] = 0;
                            $result['msg'] = "重置成功";
                        }else{
                            $result['status'] = 1;
                            $result['msg'] = "重置失败";
                        }
                    }
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 验证手机号是否注册
     * @queyuan
     * 2016/05/31
     * @param $phone
     */
    public function checkPhone(){
        $phone = I("param.phone");
        $phone = trim($phone);
        $result = array();
        if(!$phone){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("phone = '".$phone."' and status = 1")->find();
            if($user){
                $result['status'] = 0;
                $result['msg'] = "手机号已注册";
            }else{
                $result['status'] = 2;
                $result['msg'] = "手机号未注册";
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 验证用户名是否已使用
     * @queyuan
     * 2016/05/31
     * @param $name
     */
    public function checkUsername(){
        $username = I("param.username");
        $username = trim($username);
        $result = array();
        if(!$username){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("name = '".$username."' and status = 1")->find();
            if(!$user){
                $result['status'] = 0;
                $result['msg'] = "用户名未使用";
            }else{
                $result['status'] = 2;
                $result['msg'] = "用户名已使用";
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 检查用户是否绑定手机
     * @queyuan
     * 2016/07/03
     * @param $user_id
     */
    public function checkUserPhone(){
        $user_id = I("param.user_id");
        $phone = M("user")->where("id = ".$user_id)->getField("phone");
        $result = array();
        if($phone){
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['is_phone'] = 1;
        }else{
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['is_phone'] = 0;
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 获取用户资料
     * @queyuan
     * 2016/07/04
     * @param $user_id
     */
    public function userProfile(){
        $user_id = I("param.user_id");
        $result = array();
        if(!$user_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("id = ".$user_id)->field("id,name,phone,avatar,money,integral,avatar_type,status,birthday")->find();
            if(!$user){
                $result['status'] = 1;
                $result['msg'] = "无此用户";
            }else{
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['user'] = $user;
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 检查今日是否签到
     * @queyuan
     * 2016/07/05
     * @param $user_id
     */
    public function checkSign(){
        $user_id = I("param.user_id");
        $result = array();
        if(!$user_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $begin_time = strtotime(date("Y-m-d 00:00:00",time()));
            $end_time = strtotime(date("Y-m-d 23:59:59",time()));
            $res = M("user_sign")->where("sign_time between ".$begin_time." and ".$end_time." and user_id = ".$user_id)->count();
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['sign'] = $res;
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 用户签到
     * @queyuan
     * 2016/07/05
     * @param $user_id,$ip
     */
    public function sign(){
        $user_id = I("param.user_id");
        $ip = I("param.ip");
        $result = array();
        if(!$user_id || !$ip){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("id = ".$user_id." and status = 1")->field("id,name,phone,avatar,money,integral,avatar_type")->find();
            if(!$user){
                $result['status'] = 1;
                $result['msg'] = "无此用户";
            }else{
                $begin_time = strtotime(date("Y-m-d 00:00:00",time()));
                $end_time = strtotime(date("Y-m-d 23:59:59",time()));
                $count = M("user_sign")->where("sign_time between ".$begin_time." and ".$end_time." and user_id = ".$user_id)->count();
                if($count){
                    $result['status'] = 1;
                    $result['msg'] = "今日已签到";
                }else{
                    $data = array(
                        'user_id'=>$user_id,
                        'sign_time'=>time(),
                        'sign_ip'=>$ip,
                    );
                    $res = M("user_sign")->add($data);
                    if($res){
                        $integral = M("user")->where("id = ".$user_id)->getField("integral");
                        M("user")->where("id = ".$user_id)->save(array('integral'=>($integral+10)));
                        $log = new UserLogModel();
                        $log->addLog($user_id,"用户：".$user['name']."签到",0,$ip,"user_sign");
                        $result['status'] = 0;
                        $result['msg'] = "签到成功";
                    }else{
                        $result['status'] = 1;
                        $result['msg'] = "签到失败";
                    }
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 记录登出
     * @queyuan
     * 2016/07/08
     * @param $user_id,$ip
     */
    public function logout(){
        $user_id = I("param.user_id");
        $ip = I("param.ip");
        $result = array();
        if(!$user_id || !$ip){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("id = ".$user_id)->find();
            if(!$user){
                $result['status'] = 1;
                $result['msg'] = "无此用户";
            }else{
                $log = new UserLogModel();
                $log->addLog($user_id,"用户：".$user['name']."登出",0,$ip,"user");
                $result['status'] = 0;
                $result['msg'] = "操作成功";
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 修改用户资料
     * @queyuan
     * 2016/07/11
     * @param $flag:name-用户名,sex-性别（0-女，1-男）,birthday-生日（时间戳格式）,$user_id,$content,$ip
     */
    public function updateUser(){
        $flag = I("param.flag");
        $user_id = I("param.user_id");
        $content = I("param.content");
        $ip = I("param.ip");
        $result = array();
        if(!$flag || !$user_id || !$content || !$ip){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $user = M("user")->where("id = ".$user_id." and status != 0")->find();
            if(!$user){
                $result['status'] = 1;
                $reseult['msg'] = "无此用户";
            }else{
                $data = array($flag=>$content);
                $res = M("user")->where("id = ".$user_id)->save($data);
                if($res){
                    $log = new UserLogModel();
                    $log->addLog($user['id'],"用户：".$user['name']."修改资料：".$flag,0,$ip,"user");
                    $result['status'] = 0;
                    $result['msg'] = "修改成功";
                }else{
                    $result['status'] = 1;
                    $result['msg'] = "修改失败";
                }
            }
        }
        echo json_encode($result);
        exit;
    }
}
?>